package mvc;

import geographer.*;
import geographer.Database.Region;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.awt.ScrollPane;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.*;

/**
 * Project #4
 * CS 2334, 010
 * 04/02/2014
 * <P>
 * This class represents the selection view for the user
 * to enter and edit information in the Database.
 * </P>
 * @version 1.0
 */
public class SelectionView extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3052053582722316060L;

	/*The title of the view window*/
	final String TITLE = "GeoGrapher";
	
	/*The list of continents that have been loaded*/
	JList<String> continentsList;
	
	/*The list of countries that have been loaded*/
	JList<String> countriesList;
	
	/*The list of cities that have been loaded*/
	JList<String> citiesList;
	
	/*The list of points of interest that have been loaded*/
	JList<String> pointOIList;
	
	/*The list of places of interest that have been loaded*/
	JList<String> placeOIList;
	
	/*The file chooser for the user to pick and load a file*/
	JFileChooser chooser;
	
	//The list of menu items
	List<JMenuItem> menuItems = new ArrayList<JMenuItem>();
	
	//The list of butons
	List<JButton> buttonList = new ArrayList<JButton>();
	
	//The model of the region
	private RegionModel model;
	
	//The list of the indicated region
	List<String> indicatedRegion;
	
	//The map of regions
	Map<String, GeographicRegion> regions;
	
	/**
	 * The constructor for the <code>SelectionView</code>.
	 * Build the GUI for the user to interact with the Database.
	 */
	public SelectionView(){	
		
		JMenuBar menuBar = new JMenuBar();
			menuBar.add(constructFileMenu());
			menuBar.add(constructGraphMenu());
		
			
		continentsList = new JList<String>(GeoGrapher.data.getKeys(Database.Region.CONTINENT).
				toArray(new String[GeoGrapher.data.getKeys(Database.Region.CONTINENT).size()]));
		countriesList = new JList<String>(GeoGrapher.data.getKeys(Database.Region.COUNTRY).
				toArray(new String[GeoGrapher.data.getKeys(Database.Region.COUNTRY).size()]));
		citiesList = new JList<String>(GeoGrapher.data.getKeys(Database.Region.CITY).
				toArray(new String[GeoGrapher.data.getKeys(Database.Region.CITY).size()]));
		placeOIList = new JList<String>(GeoGrapher.data.getKeys(Database.Region.PLACE).
				toArray(new String[GeoGrapher.data.getKeys(Database.Region.PLACE).size()]));
		pointOIList = new JList<String>(GeoGrapher.data.getKeys(Database.Region.POINT).
				toArray(new String[GeoGrapher.data.getKeys(Database.Region.POINT).size()]));
		
		JPanel cntntGroup = new JPanel(new BorderLayout());
			cntntGroup.add(new JLabel("Continent", null, JLabel.CENTER), BorderLayout.NORTH);
			cntntGroup.add(new JScrollPane(continentsList), BorderLayout.CENTER);
			cntntGroup.add(controlButtons(Database.Region.CONTINENT), BorderLayout.SOUTH);
		JPanel cntryGroup = new JPanel(new BorderLayout());
			cntryGroup.add(new JLabel("Country", null, JLabel.CENTER), BorderLayout.NORTH);
			cntryGroup.add(new JScrollPane(countriesList), BorderLayout.CENTER);
			cntryGroup.add(controlButtons(Database.Region.COUNTRY), BorderLayout.SOUTH);
		JPanel ctyGroup = new JPanel(new BorderLayout());
			ctyGroup.add(new JLabel("City", null, JLabel.CENTER), BorderLayout.NORTH);
			ctyGroup.add(new JScrollPane(citiesList), BorderLayout.CENTER);
			ctyGroup.add(controlButtons(Database.Region.CITY), BorderLayout.SOUTH);
		JPanel plcGroup = new JPanel(new BorderLayout());
			plcGroup.add(new JLabel("Place", null, JLabel.CENTER), BorderLayout.NORTH);
			plcGroup.add(new JScrollPane(placeOIList), BorderLayout.CENTER);
			plcGroup.add(controlButtons(Database.Region.PLACE), BorderLayout.SOUTH);
		JPanel pntGroup = new JPanel(new BorderLayout());
			pntGroup.add(new JLabel("Point", null, JLabel.CENTER), BorderLayout.NORTH);
			pntGroup.add(new JScrollPane(pointOIList), BorderLayout.CENTER);
			pntGroup.add(controlButtons(Database.Region.POINT), BorderLayout.SOUTH);
			
		JPanel listPanel = new JPanel(new GridLayout(1,5,5,5));
			listPanel.add(cntntGroup);
			listPanel.add(cntryGroup);
			listPanel.add(ctyGroup);
			listPanel.add(plcGroup);
			listPanel.add(pntGroup);
			

		setSize(1000, 500);
		setLayout(new BorderLayout());
		this.setLocationRelativeTo(null);
		add(menuBar,BorderLayout.NORTH);
		add(listPanel, BorderLayout.CENTER);
		setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * The action listener for the class
	 * @param	e	The event to handle
	 */
	public void actionPerformed(ActionEvent e) {
		continentsList.setListData(GeoGrapher.data.getKeys(Database.Region.CONTINENT)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.CONTINENT).size()]));
		if(continentsList.getModel().getSize()!=0)
		{
			getButtons().get(1).setEnabled(true);
			getButtons().get(2).setEnabled(true);
		}
		countriesList.setListData(GeoGrapher.data.getKeys(Database.Region.COUNTRY)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.COUNTRY).size()]));
		if(countriesList.getModel().getSize()!=0)
		{
			getButtons().get(4).setEnabled(true);
			getButtons().get(5).setEnabled(true);
		}
		citiesList.setListData(GeoGrapher.data.getKeys(Database.Region.CITY)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.CITY).size()]));
		if(citiesList.getModel().getSize()!=0)
		{
			getButtons().get(7).setEnabled(true);
			getButtons().get(8).setEnabled(true);
		}
		placeOIList.setListData(GeoGrapher.data.getKeys(Database.Region.PLACE)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.PLACE).size()]));
		if(placeOIList.getModel().getSize()!=0)
		{
			getButtons().get(10).setEnabled(true);
			getButtons().get(11).setEnabled(true);
		}
		pointOIList.setListData(GeoGrapher.data.getKeys(Database.Region.POINT)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.POINT).size()]));
		if(pointOIList.getModel().getSize()!=0)
		{
			getButtons().get(13).setEnabled(true);
			getButtons().get(14).setEnabled(true);
		}
		getJMenuItems().get(1).setEnabled(true);
		getJMenuItems().get(3).setEnabled(true);
		pack();
	}
	/**
	 * Set the model
	 * @param model	The model to set
	 */
	public void setModel(RegionModel model) {
		this.model = model;
		model.addActionListener(this);
		continentsList.setListData(GeoGrapher.data.getKeys(Database.Region.CONTINENT)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.CONTINENT).size()]));
		countriesList.setListData(GeoGrapher.data.getKeys(Database.Region.COUNTRY)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.COUNTRY).size()]));
		citiesList.setListData(GeoGrapher.data.getKeys(Database.Region.CITY)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.CITY).size()]));
		placeOIList.setListData(GeoGrapher.data.getKeys(Database.Region.PLACE)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.PLACE).size()]));
		pointOIList.setListData(GeoGrapher.data.getKeys(Database.Region.POINT)
				.toArray(new String[GeoGrapher.data.getKeys(Database.Region.POINT).size()]));
	}
	
	/**
	 * To control the buttons
	 * @param r	the region to control
	 * @return	p	a JPanel
	 */
	private JPanel controlButtons(Database.Region r){
		JPanel p = new JPanel(new GridLayout(0,1,3,3));
		JButton addButton = new JButton("Add");
		addButton.setActionCommand(r + " Add");
		addButton.addActionListener(this);
		buttonList.add(addButton);
		JButton editButton = new JButton("Edit");
		editButton.setActionCommand(r + " Edit");
		editButton.addActionListener(this);
		editButton.setEnabled(false);
		buttonList.add(editButton);
		JButton deleteButton = new JButton("Delete");
		deleteButton.setActionCommand(r + " Delete");
		deleteButton.addActionListener(this);
		deleteButton.setEnabled(false);
		buttonList.add(deleteButton);
		
		p.add(addButton);
		p.add(editButton);
		p.add(deleteButton);
		return p;
	}
	
	/**
	 * A method to add the hybrid menu items
	 * @param menu	The menu to add them to
	 */
	private void addHybridMenuItems(JMenu menu)
	{
		JMenuItem countriesWContinents = new JMenuItem("Countries Within A Continent");
		countriesWContinents.setActionCommand("Stacked "+menu.getText()+" "+countriesWContinents.getText());
		menuItems.add(countriesWContinents);
		JMenuItem citiesWCountries = new JMenuItem("Cities Within A Country");
		citiesWCountries.setActionCommand("Stacked "+menu.getText()+" "+citiesWCountries.getText());
		menuItems.add(citiesWCountries);
		menu.add(countriesWContinents);
		menu.add(citiesWCountries);
		if(menu.getText().equals("Area")){
		JMenuItem placesOIWContinents = new JMenuItem("Places of Interest Within A Continent");
		placesOIWContinents.setActionCommand("Stacked "+menu.getText()+" "+placesOIWContinents.getText());
		menuItems.add(placesOIWContinents);
		JMenuItem placesOIWCountries = new JMenuItem("Places of Interest Within A Country");
		placesOIWCountries.setActionCommand("Stacked "+menu.getText()+" "+placesOIWCountries.getText());
		menuItems.add(placesOIWCountries);
		JMenuItem placesOFWCities = new JMenuItem("Places of Interest Within A City");
		placesOFWCities.setActionCommand("Stacked "+menu.getText()+" "+placesOFWCities.getText());
		menuItems.add(placesOFWCities);
		menu.add(placesOIWContinents);
	    menu.add(placesOIWCountries);
		menu.add(placesOFWCities);
		}
		
		
	}
	
	/**
	 * Add a single menu item
	 * @param menu	The menu to add to
	 */
	private void addSingleMenuItems(JMenu menu)
	{
		JMenuItem continents = new JMenuItem("Continents");
		continents.setActionCommand("Bar "+menu.getText()+" "+continents.getText());
		menuItems.add(continents);
		JMenuItem countries = new JMenuItem("Countries");
		countries.setActionCommand("Bar "+menu.getText()+" "+countries.getText());
		menuItems.add(countries);
		JMenuItem cities = new JMenuItem("Cities");
		cities.setActionCommand("Bar "+menu.getText()+" "+cities.getText());
		menuItems.add(cities);
		JMenuItem placesOI = new JMenuItem("Places of Interest");
		placesOI.setActionCommand("Bar "+menu.getText()+" "+placesOI.getText());
		menuItems.add(placesOI);
		
		menu.add(continents);
		menu.add(countries);
		menu.add(cities);
		menu.add(placesOI);
	}
	
	/**
	 * Add simple item menu items
	 * @param menu	The menu to add to
	 */
	private void addSimpleMenuItems(JMenu menu)
	{	
		JMenu area = new JMenu("Area");
		addSingleMenuItems(area);
		
		JMenu population = new JMenu("Population");
		addSingleMenuItems(population);
		
		menu.add(area);
		menu.add(population);
	}
	
	/**
	 * Add the file menu options
	 * @param menu The menu to add to
	 */
	private void addFileMenuOptions(JMenu menu)
	{
		JMenuItem loadGeography = new JMenuItem("Load");
		loadGeography.setActionCommand("Load");
		menuItems.add(loadGeography);
		JMenuItem saveGeography = new JMenuItem("Save");
		saveGeography.setActionCommand("Save");
		saveGeography.setEnabled(false);
		menuItems.add(saveGeography);
		JMenuItem importGeography = new JMenuItem("Import");
		importGeography.setActionCommand("Import");
		menuItems.add(importGeography);
		JMenuItem exportGeography = new JMenuItem("Export");
		exportGeography.setActionCommand("Export");
		exportGeography.setEnabled(false);
		menuItems.add(exportGeography);
		
		menu.add(loadGeography);
		menu.add(saveGeography);
		menu.add(importGeography);
	    menu.add(exportGeography);
	}
	
	/**
	 * Add the stacked menu
	 * @param menu	Menu to add to
	 */
	private void addStackedMenuItems(JMenu menu)
	{	
		JMenu area = new JMenu("Area");
		addHybridMenuItems(area);
		
		JMenu population = new JMenu("Population");
		addHybridMenuItems(population);
		
		menu.add(area);
		menu.add(population);
	}
	
	/**
	 * Add the map items
	 * @param menu Add the map items
	 */
	private void addMapItems(JMenu menu)
	{
		JMenuItem citiesWCountries = new JMenuItem("Cities Within Countries");
		citiesWCountries.setActionCommand(menu.getText()+" "+citiesWCountries.getText());
		menuItems.add(citiesWCountries);
		JMenuItem citiesWorldWide = new JMenuItem("Cities Worldwide");
		citiesWorldWide.setActionCommand(menu.getText()+" "+citiesWorldWide.getText());
		menuItems.add(citiesWorldWide);
		JMenuItem citiesWContinents = new JMenuItem("Cities Within Continents");
		citiesWContinents.setActionCommand(menu.getText()+" "+citiesWContinents.getText());
		menuItems.add(citiesWContinents);
		JMenuItem pOIWCities = new JMenuItem("Points of Interest Within Cities");
		pOIWCities.setActionCommand(menu.getText()+" "+pOIWCities.getText());
		menuItems.add(pOIWCities);
		JMenuItem pOIWCountries = new JMenuItem("Points of Interest Within Countries");
		pOIWCountries.setActionCommand(menu.getText()+" "+pOIWCountries.getText());
		menuItems.add(pOIWCountries);
		JMenuItem pOIWContinents = new JMenuItem("Points of Interest Within Continents");
		pOIWContinents.setActionCommand(menu.getText()+" "+pOIWContinents.getText());
		menuItems.add(pOIWContinents);
		JMenuItem pOIWorldWide = new JMenuItem("Points of Interest Worldwide");
		pOIWorldWide.setActionCommand(menu.getText()+" "+pOIWorldWide.getText());
		menuItems.add(pOIWorldWide);
		
		menu.add(citiesWCountries);
		menu.add(citiesWorldWide);
		menu.add(citiesWContinents);
		menu.add(pOIWCities);
		menu.add(pOIWCountries);
		menu.add(pOIWContinents);
		menu.add(pOIWorldWide);
	}
	
	/**
	 * Add the graph menu items
	 * @param menu	The menu to add to
	 */
	private void addGraphMenuOptions(JMenu menu)
	{
		JMenu simpleBar = new JMenu("Simple Bar Chart");
		JMenu stackedBar = new JMenu("Stacked Bar Chart");
		JMenu map = new JMenu("Map");
		
		addStackedMenuItems(stackedBar);
		addSimpleMenuItems(simpleBar);
		addMapItems(map);
		
		menu.add(simpleBar);
		menu.add(stackedBar);
		menu.add(map);
	}
	
	/**
	 * Make the file menu
	 * @return fileMenu	the menu
	 */
	private JMenu constructFileMenu()
	{
		JMenu fileMenu = new JMenu("File");
		addFileMenuOptions(fileMenu);
		return fileMenu;
	}
	
	/**
	 * Construct the graph menu
	 * @return graphMenu	The graph menu
	 */
	private JMenu constructGraphMenu()
	{
		JMenu graphMenu = new JMenu("Graph");
		addGraphMenuOptions(graphMenu);
		return graphMenu;
	}
	
	/**
	 * The getter for JMenu
	 * @return menuItems	Menu Items
	 */
	public List<JMenuItem> getJMenuItems()
	{
		return this.menuItems;
	}
	
	/**
	 * Getter for buttons
	 * @return buttonList	The list of Buttons
	 */
	public List<JButton> getButtons()
	{
		return this.buttonList;
	}
	
	/**
	 * Getter for model
	 * @return model	The model
	 */
	public RegionModel getModel()
	{
		return this.model;
	}
	
	/**
	 * Get the view
	 * @return this	the view
	 */
	public SelectionView getView()
	{
		return this;
	}
	
	/**
	 * A new class for the button listener
	 *
	 */
	public class ButtonListener implements ActionListener
	{
		/**
		 * The Action Listener
		 * @param	actionEvent	The Action Event
		 */
		public void actionPerformed(ActionEvent actionEvent)
		{
			actionEvent.getActionCommand().trim();
			switch(actionEvent.getActionCommand())
			{
				case "CONTINENT Add":
					new AddDialog(Database.Region.CONTINENT, getView());
					break;
				case "CONTINENT Delete":
					List<String> deleteKeys = continentsList.getSelectedValuesList();
					for(String deleteKey: deleteKeys)
						model.delete(Database.Region.CONTINENT, deleteKey);
					break;
				case "CONTINENT Edit":
					List<String> key = continentsList.getSelectedValuesList();
					for(String k: key)
					new EditDialog(Database.Region.CONTINENT,getView(),k);
					break;
				case "COUNTRY Add":
					new AddDialog(Database.Region.COUNTRY, getView());
					break;
				case "COUNTRY Delete":
					List<String> deleteKeys1 = countriesList.getSelectedValuesList();
					for(String deleteKey1: deleteKeys1)
					model.delete(Database.Region.COUNTRY, deleteKey1);
					break;
				case "COUNTRY Edit":
					List<String> key1 = countriesList.getSelectedValuesList();
					for(String k1: key1)
					new EditDialog(Database.Region.COUNTRY,getView(),k1);
					break;
				case "CITY Add":
					new AddDialog(Database.Region.CITY, getView());
					break;
				case "CITY Delete":
					List<String> deleteKeys2 = citiesList.getSelectedValuesList();
					for(String deleteKey2: deleteKeys2)
					model.delete(Database.Region.CITY, deleteKey2);
					break;
				case "CITY Edit":
					List<String> key2 = citiesList.getSelectedValuesList();
					for(String k2: key2)
					new EditDialog(Database.Region.CITY,getView(),k2);
					break;
				case "PLACE Add":
					new AddDialog(Database.Region.PLACE, getView());
					break;
				case "PLACE Delete":
					List<String> deleteKeys3 = placeOIList.getSelectedValuesList();
					for(String deleteKey3: deleteKeys3)
					model.delete(Database.Region.PLACE, deleteKey3);
					break;
				case "PLACE Edit":
					List<String> key3 = placeOIList.getSelectedValuesList();
					for(String k3: key3)
					new EditDialog(Database.Region.PLACE,getView(),k3);
					break;
				case "POINT Add":
					new AddDialog(Database.Region.POINT,getView());
					break;
				case "POINT Edit":
					List<String> key4 = placeOIList.getSelectedValuesList();
					for(String k4: key4)
					new EditDialog(Database.Region.POINT,getView(),k4);
					break;
				case "POINT Delete":
					List<String> deleteKeys4 = citiesList.getSelectedValuesList();
					for(String deleteKey4: deleteKeys4)
					model.delete(Database.Region.POINT, deleteKey4);
					break;
			}
		}
	}
	
	/**
	 * The class for the menu listener
	 *
	 */
	public class MenuListener implements ActionListener
	{
		/**
		 * The listener for the menu
		 */
		@SuppressWarnings({ "unchecked", "rawtypes" })
		public void actionPerformed(ActionEvent actionEvent)
		{
			JPanel graph;
			actionEvent.getActionCommand().trim();
			switch(actionEvent.getActionCommand())
			{
				case "Stacked Area Countries Within A Continent":
					
					new SelectContainer(Region.CONTINENT, "Area", getView());
					break;
					
				case "Stacked Area Cities Within A Country":
					new SelectContainer(Region.COUNTRY, "Area", getView());
					break;
				case "Stacked Area Places of Interest Within A Continent":
				
				case "Stacked Area Places of Interest Within A Country":
					
				case "Stacked Area Places of Interest Within A City":
					
				case "Stacked Population Countries Within A Continent":
					new SelectContainer(Region.CONTINENT, "Population", getView());
					break;
				case "Stacked Population Cities Within A Country":
					new SelectContainer(Region.COUNTRY, "Population", getView());
					break;
				
				case "Bar Area Continents":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					Collections.sort(indicatedRegion, Continent.AR);
					JFrame barFrame = new JFrame("GeoGrapher :: Bar");
					JPanel p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(continentsList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					
					indicatedRegion = GeoGrapher.data.getKeys(Database.Region.CONTINENT);
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getArea());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getArea()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Continents by Area", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Area Countries":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					regions = GeoGrapher.data.getRegions();
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.COUNTRY);
					Collections.sort(indicatedRegion, Country.AR);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(countriesList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getArea());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getArea()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Countries by Area", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Area Cities":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					regions = GeoGrapher.data.getRegions();
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.CITY);
					Collections.sort(indicatedRegion, City.AR);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(citiesList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getArea());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getArea()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Cities by Area", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Area Places of Interest":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					regions = GeoGrapher.data.getRegions();
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.PLACE);
					Collections.sort(indicatedRegion, Point.AR);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(pointOIList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getArea());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getArea()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Points of Interest by Area", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Population Continents":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					Collections.sort(indicatedRegion, Continent.PO);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(continentsList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getPopulation());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getPopulation()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Continents by Population", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Population Countries":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					regions = GeoGrapher.data.getRegions();
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.COUNTRY);
					Collections.sort(indicatedRegion, Country.PO);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(countriesList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getPopulation());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getPopulation()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Countries by Population", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Population Cities":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.CITY);
					regions = GeoGrapher.data.getRegions();
					Collections.sort(indicatedRegion, City.PO);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(citiesList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getPopulation());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getPopulation()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Cities by Population", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Bar Population Places of Interest":
					indicatedRegion = GeoGrapher.data.getKeys(Region.CONTINENT);
					regions = GeoGrapher.data.getRegions();
					indicatedRegion = (ArrayList) GeoGrapher.data.getKeys(Region.PLACE);
					Collections.sort(indicatedRegion, Place.PO);
					barFrame = new JFrame("GeoGrapher :: Bar");
					p = new JPanel();
					graph = new JPanel(new BorderLayout());
					p.setPreferredSize(new java.awt.Dimension(pointOIList.getModel().getSize()*125, 300));
					
					//Configure the layout and add the panel elements
					p.setLayout(new GridLayout(1,0));
					Bar.setHeightReference(Database.regions.get(indicatedRegion.get(0)).getPopulation());
					for(String r: (String[]) indicatedRegion.toArray(new String[indicatedRegion.size()]))
						p.add(new Bar(Database.regions.get(r).getName(), Database.regions.get(r).getPopulation()));
						
					//Add elements to the frame and show the frame
					graph.add(new JScrollPane(p), BorderLayout.CENTER);
					graph.add(new JLabel("Points of Interest by Population", null, JLabel.CENTER), BorderLayout.NORTH);
					barFrame.add(graph);
					barFrame.setSize(800, 550);
					barFrame.setLocationRelativeTo(null);
					barFrame.setVisible(true);
					break;
				case "Map Cities Within Countries":
					new SelectContainer(Region.COUNTRY, "Map", getView());
					break;
				case "Map Cities Worldwide":
					//Instantiate the frame
					JFrame worldFrame = new JFrame("Geographer :: World Map");
					
					//The world map to be displayed
					WorldMap map =  new WorldMap();
					
					
					
					
					//Loop through the hash map to get the relevante values
					for(String reg: Database.getKeys(Region.CITY))
					{
						//Collect then City value
						try
						{
							City city = (City)Database.regions.get(reg);
							map.addPoint(city.getName(),city.getLat(),city.getLon());
						}catch(NullPointerException e3)
						{
							//Inform the user of an error
							//System.out.println(city.getName()+" does not have an associated latitude or longitude.");
						}
					}
					graph = new JPanel(new BorderLayout());
					graph.add(map, BorderLayout.CENTER);
					graph.add(new JLabel("Cities on the World Map", null, JLabel.CENTER), BorderLayout.NORTH);
					worldFrame.add(map);
					worldFrame.setSize(1300, 685);
					worldFrame.setDefaultCloseOperation(JFrame.CROSSHAIR_CURSOR);
					worldFrame.setLocationRelativeTo(null);
					worldFrame.setVisible(true);
						break;
				case "Map Cities Within Continents":
					new SelectContainer(Region.CONTINENT, "Map", getView());
					break;
				case "Map Places of Interest Within Cities":
					break;
				case "Map Places of Interest Within Countries":
					break;
				case "Map Places of Interest Within Continents":
					break;
				case "Map Places of Interest Worldwide":
					
					break;
				case "Save": 
							RegionController.saveData(getView());
					break;
				case "Load": 
							 RegionController.loadData(getView());
					break;
				case "Import": RegionController.importData(getView());
					break;
				case "Export": RegionController.exportData(getView());
					break;
			}	
		}
	}
}
